

enum MissionStatus {


    UNACCEPTABLE = 0,

    CAN_ACCEPT = 1,

    DURGING = 2,

    CAN_SUBMIT = 3,

    FINISHIED = 4
}

class Mission {
    dialog = new Image();

    id = 0;

    name = '';

    content: string = '';
    missionContents: MissionContent[] = [];
    images: number[] = [];
    contents: string[] = [];

    canAcceptContent: string = "";

    canSubmitContent: string = "";


    parseFromConfig(config: any) {
        this.id = config.id;
        this.name = config.name;
        this.content = config.content;

        this.missionContents = config.missionContent;

        for (let item of this.missionContents) {
            const missionContent = new MissionContent();

            missionContent.parseFromConfig(item);
            this.images.push(missionContent.imageId)
            this.contents.push(missionContent.text);
        }

        this.canAcceptContent = config.canAcceptContent
        //this.canSubmitContent = config.canSubmitContent;
        this.needLevel = config.needLevel;
        this.fromNpcId = config.fromNpcId;
        this.toNpcId = config.toNpcId;

        // const dialogboxUrl = config.dialogbox;
        // const dialogboxTexture = new Image();
        // this.dialog = dialogboxTexture;
        // dialogboxTexture.src = dialogboxUrl;
        // this.dialog.src = dialogboxUrl;
    }
    // constructor(config: any) {
    //     const dialogboxUrl = config.dialogbox;
    //     const dialogboxTexture = new Image();
    //     this.dialog = dialogboxTexture;
    //     dialogboxTexture.src = dialogboxUrl;
    //     const dialogBitmap = new renderer.Bitmap(0, 0, dialogboxTexture);

    // }


    needLevel = 0;

    fromNpcId = 0;

    toNpcId = 0;

    isAccepted = false;

    isSubmit = false;

    current = 0;

    total = 1;


    // rewards: { type: string, num: number }[] = [];




    status: MissionStatus = 0;

    update() {
        let nextStatus: MissionStatus = 0;
        if (this.isSubmit) {
            nextStatus = MissionStatus.FINISHIED
        }
        else if (this.isAccepted) {
            if (this.current >= this.total) {
                nextStatus = MissionStatus.CAN_SUBMIT;
            }
            else {
                nextStatus = MissionStatus.DURGING;
            }

        }
        else {
            if (this.needLevel > user.level) {
                nextStatus = MissionStatus.UNACCEPTABLE;
            }
            else {
                nextStatus = MissionStatus.CAN_ACCEPT;
            }

        }


        if (nextStatus != this.status) {
            this.status = nextStatus;
            return true;
        }
        else {
            return false;
        }

    }
}


class MissionManager extends EventDispatcher {


    missions: Mission[] = [];

    constructor() {
        super();
    }

    acceptMission(mission: Mission) {
        mission.isAccepted = true;
        mission.current = 1;
        this.update();
    }

    parseFromConfig(config: any) {
        for (let item of config.tasks) {
            const mission1 = new Mission();
            mission1.parseFromConfig(item);
            missionManager.missions.push(mission1);
        }
    }

    submitMission(mission: Mission) {
        mission.isSubmit = true;

        this.update();

    }

    init() {
        user.addEventListener("userchange", () => {
            this.update();
        });
        this.update();
    }

    update() {
        let needUpdate = false;
        for (let mission of this.missions) {
            const missionNeedUpdate = mission.update();
            if (missionNeedUpdate) {
                needUpdate = true;
            }
        }
        if (needUpdate) {
            this.dispatchEvent("missionUpdate", {});
        }
    }


}

class MissionContent {
    imageId = 0;

    text = "";
    parseFromConfig(config: any) {
        this.imageId = config.imageId;
        //     const npc = npcManager.getNpcById(config.imageId);
        this.text = config.text;
    }

}
const missionManager = new MissionManager();